package com.ten.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.ten.common.core.entity.EduVideo;
import com.ten.common.core.vo.Result;
import com.ten.course.dao.CourseVideoDao;
import com.ten.course.service.CourseVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/22 12:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CourseVideoServiceImpl implements CourseVideoService {
    @Autowired
    private CourseVideoDao courseVideoDao;

    /**
     * 根据chapterId查询出章节下的视频
     *
     * @param chapterId 章节id
     * @return
     */
    @Override
    public Result<List<EduVideo>> findAllVideo(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        List<EduVideo> eduVideos = courseVideoDao.selectList(wrapper);
        return new Result<>(200, "查询成功", eduVideos);
    }

    //    isFree
//    title
//    videoSourceId
//    videoOriginalName
//    chapterId
//    courseId
//    sort
    @Override
    public Result insertVideo(EduVideo eduVideo) {
        int insert = courseVideoDao.insert(eduVideo);
        if(insert>0){
            return new Result(200,"保存成功");
        }
        return new Result(500,"保存失败");
    }

    @Override
    public Result updateVideo(EduVideo eduVideo) {
        UpdateWrapper<EduVideo> wrapper =new UpdateWrapper<>();
        wrapper.eq("id",eduVideo.getId());
        int update = courseVideoDao.update(eduVideo, wrapper);
        if(update>0){
            return new Result(200,"修改成功");
        }
        return new Result(500,"修改失败");
    }

    @Override
    public Result deleteById(String id, Integer sort, String chapterId) {
        int i = courseVideoDao.deleteById(id);
        if(i>0){
            return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    /**
     * 根据课程id查询出所有章节的视频信息
     * @param courseId 课程id
     * @return
     */
    @Override
    public Result<List<EduVideo>> queryAllVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper =new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = courseVideoDao.selectList(wrapper);
        return new Result<>(200,"查询成功",eduVideos);
    }
}